#include "IntersectionInfoList.h"

IntersectionInfoList::IntersectionInfoList(Line *line1, Line *line2, IntersectionType itype) {
  this->l1 = line1;
  this->l2 = line2;
  intersectionType = itype;
  next = this;
  prev = this;
}
IntersectionInfoList::IntersectionInfoList() {
  l1 = NULL;
  l2 = NULL;
  next = this;
  prev = this;
}

void IntersectionInfoList::append(IntersectionInfoList *right) {
  //this is identity, take rights data
  if(!l1 || !l2) {
    l1 = right->l1;
    l2 = right->l2;
    intersectionType = right->intersectionType;
    //taking single ele list
    if(right->next == right) {
      next = this;
      prev = this;
      return;
    }

    next = right->next;
    prev = right->prev;
    right->prev->next = this;
    right->next->prev = this;
    return;
    
  }
  //right is identity, do nothing
  if(!(right->l1) || ! (right->l2) ) return;

  //find last item
  IntersectionInfoList *lLastElement = prev;

  //right element is detoryed after reduce
  IntersectionInfoList* rightCopy = new IntersectionInfoList(right->l1, right->l2,
      right->intersectionType);
  //check for single element list, double linked list with dupe
  //is an enormous pain
  if(right->next != right) {
    rightCopy->next = right->next;
    rightCopy->prev = right->prev;
  }
  //maintain pointer of right's next element
  right->next->prev = rightCopy;
  right->prev->next = rightCopy;

  IntersectionInfoList *rLastElement = rightCopy->prev;

  rightCopy->prev = lLastElement;

  //set last elements next to be the first element of right list
  lLastElement->next = rightCopy;
  //right's next is now us
  rLastElement->next = this;
  //our previous is now the last of r
  prev = rLastElement;
  
}
